package com.cn.web.controller.ComponentMechanical;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.ComponentMechanical.OutsourcingResult;
import com.cn.bean.pojo.TechnologicalStandard.GrindingProcessStandardMain;
import com.cn.bean.pojo.workOrder.*;
import com.cn.bean.vo.Equipment.BusinessDimensionConfiguration;
import com.cn.bean.vo.Equipment.EquipmentStructure;
import com.cn.config.config.Result;
import com.cn.service.server.ComponentMechanical.OutsourcingResultService;
import com.cn.service.server.Equipment.BusinessDimensionConfigurationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Orc")
@Transactional
public class OutsourcingResultController {
    private final OutsourcingResultService outsourcingResultService;

    @Autowired(required = false)
    public OutsourcingResultController(OutsourcingResultService outsourcingResultService) {
        this.outsourcingResultService = outsourcingResultService;
    }

    @RequestMapping("/select/OutsourcingResultList")
    @ResponseBody
    public List<OutsourcingResult> selectOutsourcingResultList(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String processName = jsonObject.getString("inputName");
        processName = (processName != null) ? processName : "";

        return outsourcingResultService.selectOutsourcingResultList(page, limit, processName);
    }

    @RequestMapping("/select/GrindingReturn")
    @ResponseBody
    public List<OutsourcingResultSubTable> selectGrindingReturn(@RequestBody OutsourcingResultSubTable outsourcingResultSubTable) {
        return outsourcingResultService.selectGrindingReturn(outsourcingResultSubTable);
    }

    //委外出库确认
    @RequestMapping("/insert/Outsourcing")
    @ResponseBody
    public Result insertOutsourcing(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("childList");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        OutsourcingResultMainTable mainTable = new OutsourcingResultMainTable();
        Integer id = outsourcingResultService.getOutsourcingResultMainAutoid();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        String supplier = jsonObject.getString("supplier");
        String supplierAddress = jsonObject.getString("supplierAddress");
        int workOrderNumber = jsonObject.getInteger("workOrderCode");
        int grindingProcessStandard = jsonObject.getInteger("grindingProcessStandard");
        mainTable.setId(id);
        mainTable.setSupplier(supplier);
        mainTable.setSupplierAddress(supplierAddress);
        mainTable.setWorkOrderNumber(workOrderNumber);
        mainTable.setCreator(userName);
        mainTable.setCreationTime(format.format(date));
        mainTable.setWorkOrderCode(workOrderNumber);
        mainTable.setGrindingProcessStandard(grindingProcessStandard);
        int a = outsourcingResultService.insertOutsourcingResultMain(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<OutsourcingResultSubTable> djBhList = dataArray.toJavaList(OutsourcingResultSubTable.class);
            for (OutsourcingResultSubTable dataStructureDto : djBhList) {
                OutsourcingResultSubTable subTable = new OutsourcingResultSubTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = outsourcingResultService.getOutsourcingResultSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                int b = outsourcingResultService.insertOutsourcingResultSub(subTable);
                if (b != 1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success("200", "出库成功");
    }

    //委外入库确认
    @RequestMapping("/insert/GrindingReturnSub")
    @ResponseBody
    public Result insertGrindingReturnSub(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("childList");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        OutsourcingResultMainTable mainTable = new OutsourcingResultMainTable();
        Integer id = jsonObject.getInteger("id");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int workOrderNumber = jsonObject.getInteger("workOrderCode");
        mainTable.setId(id);
        mainTable.setWorkOrderNumber(workOrderNumber);
        mainTable.setReturnTime(format.format(date));
        int a = outsourcingResultService.updateGrindingReturnSub(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        List<OutsourcingResultSubTable>subTableList=outsourcingResultService.selectGrindingReturnSub(id);
        if (subTableList.size()>0){
            int v=outsourcingResultService.delOutsourcing(id);
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<GrindingReturnSubTable> djBhList = dataArray.toJavaList(GrindingReturnSubTable.class);
            for (GrindingReturnSubTable dataStructureDto : djBhList) {
                GrindingReturnSubTable subTable = new GrindingReturnSubTable();
                OutsourcingResultSubTable outTable = new OutsourcingResultSubTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = outsourcingResultService.getGrindingReturnSubTableAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                subTable.setReturnTime(format.format(date));
                outTable.setMid(id);
                outTable.setGrindingCapacity(subTable.getGrindingCapacity());
                outTable.setRoughnessStandard(subTable.getRoughnessStandard());
                int b = outsourcingResultService.insertGrindingReturnSub(subTable);
                int c = outsourcingResultService.updateOutsourcingResultSub(outTable);
                if (b != 1 && c != 1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success("200", "入库成功");
    }

    @RequestMapping("/update/GrindingReturn")
    @ResponseBody
    public Result updateGrindingReturnAudit(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        JSONArray workArray = jsonObject.getJSONArray("workOrderCode");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            List<Integer> workList = workArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                outsourcingResultService.GrindingReturnAudit(id);
                for (Integer workOrderNumber : workList) {
                    outsourcingResultService.completeOutsourcingWorkOrde(workOrderNumber);
                }
            }
            return Result.success("200", "审核成功");
        }
        return Result.failure("500", "审核失败");
    }

    @RequestMapping("/select/GrindingProcessStandard")
    @ResponseBody
    public List<GrindingProcessStandardMain> selectGrindingProcessStandard(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");

        return outsourcingResultService.selectGrindingProcessStandard(id);
    }

    @RequestMapping("/update/OutsourcingResultAutoUniqueCode")
    @ResponseBody
    public Result updateOutsourcingResultAutoUniqueCode(@RequestBody JSONObject jsonObject) {
        JSONArray jsonArray = jsonObject.getJSONArray("uniqueCodeList");
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            stringList.add(jsonArray.getString(i));
        }
        return outsourcingResultService.updateOutsourcingResultAutoUniqueCode(stringList);
    }
}
